package com.icebergteam.warfacewidget.core.settings;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;
import com.icebergteam.warfacewidget.R;
import android.content.Context;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.TimeDateUtils;

public class NewsSettings extends Settings {
	public final static String className = NewsSettings.class.getCanonicalName();

	public final static String PREFS_FILE_NAME = "news_settings.ini";
	// Data update
	boolean rssAutoupdate = true;
	private final int MIN_UPDATE_PERIOD_ID = 0;
	private final int MAX_UPDATE_PERIOD_ID = 1;
	private final int periods[] = { 1, 24 };
	
	private ArrayList<String> categories = new ArrayList<String>();
	
	private int rssUpdatePeriod = periods[0];
	private long rssLastUpdateTimestamp = 0;
	// representation
	private boolean displayNewsFromSite	= true;
	private boolean rssThumbnails		= true;
	//cache
	private long periodHistory = 2 * 604800 * 1000;
	
	private int countViewNews = 20;

	public boolean isRSSThumbnails() {
		return this.rssThumbnails;
	}

	public void setRSSThumbnails(boolean rssThumbnails) {
		this.rssThumbnails = rssThumbnails;
	}

	public boolean isDisplayNewsFromSite() {
		return this.displayNewsFromSite;
	}

	public void setDisplayNewsFromSite(boolean displayNewsFromSite) {
		this.displayNewsFromSite = displayNewsFromSite;
	}

	public long getRSSLastUpdateTimestamp() {
		return rssLastUpdateTimestamp;
	}

	public void setRSSLastUpdateTimestamp(long m_rssLastUpdateTimestamp) {
		this.rssLastUpdateTimestamp = m_rssLastUpdateTimestamp;
	}

	public boolean isRSSAutoupdate() {
		return this.rssAutoupdate;
	}

	public void setRSSAutoupdate(boolean autoupadate) {
		this.rssAutoupdate = autoupadate;
	}

	public int getMinUpdatePeriod() {
		return this.periods[MIN_UPDATE_PERIOD_ID];
	}

	public int getMaxUpdatePeriod() {
		return this.periods[MAX_UPDATE_PERIOD_ID];
	}

	public int getRSSUpdatePeriod() {
		return this.rssUpdatePeriod;
	}

	public void setRSSUpdatePeriod(int updatePeriod) {
		this.rssUpdatePeriod = updatePeriod;
	}

	public NewsSettings() {
		Context context = AppManager.getContext();
		
		init(context);
		load();
	
//		categories = null;
//		categories = new ArrayList<String>();
//		categories = AppManager.getDatabase().getNewsTable().getCategories();
	}

	@Override
	public void save() {
		Context context = AppManager.getContext();
		try {
			FileOutputStream fos = context.openFileOutput(getPrefFileName(),
					Context.MODE_PRIVATE);
			this.properties.setProperty(
					getKey(R.string.key_prefs_news_autoupdate_rss),
					String.format("%s", this.rssAutoupdate));
			this.properties.setProperty(
					getKey(R.string.key_prefs_news_autoupdate_period),
					String.format("%s", this.rssUpdatePeriod));
			this.properties.setProperty(
					getKey(R.string.key_prefs_news_last_update_timestamp),
					String.format("%s", this.rssLastUpdateTimestamp));
			this.properties.setProperty(
					getKey(R.string.key_prefs_news_display_news_from_site),
					String.format("%s", this.displayNewsFromSite));
			this.properties.setProperty(
					getKey(R.string.key_prefs_news_display_rss_thumbnails),
					String.format("%s", this.rssThumbnails));
			
			this.properties.store(fos, "wotwidget");
			fos.close();
			LogManager.LogV(NewsSettings.className, LogManager.getExecuteMethodName(), this.rssAutoupdate,
					this.rssUpdatePeriod, this.rssLastUpdateTimestamp, TimeDateUtils.getDateByLocale(new Date(this.rssLastUpdateTimestamp), Locale.getDefault(),null), this.properties.toString());
		} catch (FileNotFoundException e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName() + " can't read pref file",
					e.getMessage());
			e.printStackTrace();
		} catch (Exception e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName(),
					e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void load() {
		
		String autoupdate = this.properties.getProperty(
				getKey(R.string.key_prefs_news_autoupdate_rss),
				String.format("%s", this.rssAutoupdate));
		String updatePeriod = this.properties.getProperty(
				getKey(R.string.key_prefs_news_autoupdate_period),
				String.format("%s", this.rssUpdatePeriod));
		String rssLastUpdateTimestamp = this.properties.getProperty(
				getKey(R.string.key_prefs_news_last_update_timestamp),
				String.format("%s", this.rssLastUpdateTimestamp));
		String displayNewsFromSite = this.properties.getProperty(
				getKey(R.string.key_prefs_news_display_news_from_site),
				String.format("%s", this.displayNewsFromSite));
		String rssThumbnails = this.properties.getProperty(
				getKey(R.string.key_prefs_news_display_rss_thumbnails),
				String.format("%s", this.rssThumbnails));

		try {
			this.rssAutoupdate				= Boolean.parseBoolean(autoupdate);
			this.rssUpdatePeriod			= Integer.parseInt(updatePeriod);
			this.rssLastUpdateTimestamp		= Long.parseLong(rssLastUpdateTimestamp);
			this.displayNewsFromSite		= Boolean.parseBoolean(displayNewsFromSite); 
			this.rssThumbnails				= Boolean.parseBoolean(rssThumbnails);			

			LogManager.LogV(NewsSettings.className, LogManager.getExecuteMethodName(), this.rssAutoupdate,
					this.rssUpdatePeriod, this.rssLastUpdateTimestamp, TimeDateUtils.getDateByLocale(new Date(this.rssLastUpdateTimestamp), Locale.getDefault(), null), this.properties.toString());
		} catch (Exception e) {
			LogManager.LogE(NewsSettings.className, LogManager.getExecuteMethodName(), e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * @return the countViewNews
	 */
	public int getCountViewNews() {
		return countViewNews;
	}

	/**
	 * @param countViewNews the countViewNews to set
	 */
	void setCountViewNews(int countViewNews) {
		this.countViewNews = countViewNews;
	}

	/**
	 * @return the periodHistory
	 */
	public long getPeriodHistory() {
		return periodHistory;
	}

	/**
	 * @param periodHistory the periodHistory to set
	 */
	void setPeriodHistory(long periodHistory) {
		this.periodHistory = periodHistory;
	}
	
	public void setCategories(ArrayList<String> list){
		categories = list;
	}

	public ArrayList<String> getCategories() {
		return categories;
	}

	@Override
	public String getPrefFileName() {
		return NewsSettings.PREFS_FILE_NAME;
	}
}